#include "cw32l010_flash.h"
#include "flash_fun.h"
#include "rda5807m.h"

//访问的最高频率为24MHz，如果系统配置的HCLK时钟频率高于24MHz，则必须通过FLASH控制寄存器FLASH_CR2的WAIT位域配置合理的响应等待时间，才能保证FLASH被正确访问。
//具体为FLASH_SetLatency(FLASH_Latency_2),大于24mhz必须设置两个周期等待

//读取存储的REG设置参数
void FM_FLASH_Read_REG(void){
    fm_info.RDA5807_REG_02 = *((volatile uint16_t *)(data_save_addr));
    fm_info.RDA5807_REG_03 = *((volatile uint16_t *)(data_save_addr+2));
    fm_info.RDA5807_REG_05 = *((volatile uint16_t *)(data_save_addr+4));
}
void FM_Write_REG(void){
    FLASH_UnlockPages(data_save_addr, data_save_addr);    //解锁127页
    FLASH_ErasePages(data_save_addr,data_save_addr);//清空page,不清空写入会失败
    uint16_t data_buf[128];
    data_buf[0] = fm_info.RDA5807_REG_02;
    data_buf[1] = fm_info.RDA5807_REG_03;
    data_buf[2] = fm_info.RDA5807_REG_05;
    FLASH_WriteHalfWords(data_save_addr,(uint16_t *)data_buf,128);//半字节写入
    FLASH_LockAllPages();
}

void flash_demo(void){
    FM_FLASH_Read_REG();
    if(fm_info.RDA5807_REG_02 == 0xFFFF){
        fm_info.RDA5807_REG_02 = 0xE005; //不带低音增强,新解析启用
        fm_info.RDA5807_REG_03 = 0x0018; // 设置频率参数,开启协调，00全球波段，步进00 100khz
        fm_info.RDA5807_REG_05 = 0x848B; // 搜台SNR 4db,lan模式选择AP，lan电流选择 1.8ma，音量控制
        FM_Write_REG();
    }
    FM_FLASH_Read_REG();
}

